package com.elegantmall.service;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.jwt.JWT;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.elegantmall.dao.GoodsDao;
import com.elegantmall.dao.SellerDao;
import com.elegantmall.pojo.GoodsPojo;
import com.elegantmall.pojo.SellerPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class SellerServiceImpl {

    @Autowired
    SellerDao sellerDao;

    @Autowired
    GoodsDao goodsDao;


    public Boolean login(String sellername,String password){
        SellerPojo sellerPojo = sellerDao.selectById(sellername);
        if(sellername == null){
            return false;
        }
        else if(!sellerPojo.getPassword().equals(password)){
            return false;
        }
        else{

            return true;
        }
    }
    public SaResult register(String sellername, String password, String nickname) {
        SellerPojo sellerPojo = sellerDao.selectById(sellername);
        if (sellerPojo != null) {
            return SaResult.error("用户名已被注册");  // 返回 SaResult 错误
        } else {
            SellerPojo sellerPojo2 = new SellerPojo(sellername, password, nickname);
            sellerDao.insert(sellerPojo2);
            return SaResult.ok("注册成功");  // 返回 SaResult 成功
        }
    }

    // 库存增加方法
    public String increaseStock(String goodsId, int delta) {
        GoodsPojo goods = goodsDao.selectById(goodsId);
        if (goods == null) {
            return "商品不存在";
        }
        goods.setNumber(goods.getNumber() + delta);
        goodsDao.updateById(goods);
        return "库存增加成功";
    }

    // 库存减少方法（带校验）
    public String decreaseStock(String goodsId, int delta) {
        GoodsPojo goods = goodsDao.selectById(goodsId);
        if (goods == null) {
            return "商品不存在";
        }
        if (goods.getNumber() < delta) {
            return "库存不足";
        }
        goods.setNumber(goods.getNumber() - delta);
        goodsDao.updateById(goods);
        return "库存减少成功";
    }

    public String addGoods(String sellerName, GoodsPojo goods) {
        goods.setGoodsId(STR."G-\{System.currentTimeMillis()}");
        goods.setSellerName(sellerName);
        goods.setNumber(1); // 强制设置默认库存

        goodsDao.insert(goods);
        return STR."添加成功，ID: \{goods.getGoodsId()}";
    }

    // 删除商品（校验归属）
    public String deleteGoods(String sellerName, String goodsId) {
        GoodsPojo goods = goodsDao.selectById(goodsId);
        if (goods == null || !goods.getSellerName().equals(sellerName)) {
            return "删除失败：商品不存在或无权操作";
        }

        int affected = goodsDao.deleteBySeller(goodsId, sellerName);
        return affected > 0 ? "删除成功" : "删除失败";
    }

    public List<GoodsPojo> getGoodsBySeller(String sellerName) {
        if (StringUtils.isBlank(sellerName)) {
            throw new IllegalArgumentException("商家名称不能为空");
        }

        List<GoodsPojo> goodsList = goodsDao.selectBySellerName(sellerName);

        // 可以在这里添加额外的业务逻辑，如过滤下架商品等
        // goodsList = goodsList.stream().filter(g -> g.getStatus() == 1).collect(Collectors.toList());

        return goodsList;
    }

    public String updateGoodsInfo(GoodsPojo goodsPojo) {
        // 基础参数检查
        if (goodsPojo == null || StringUtils.isBlank(goodsPojo.getGoodsId())) {
            return "商品信息不完整";
        }

        // 检查商品是否存在
        GoodsPojo existing = goodsDao.selectByGoodsId(goodsPojo.getGoodsId());
        if (existing == null) {
            return "商品不存在";
        }


        // 执行更新
        int affected = goodsDao.updateGoodsInfo(goodsPojo);
        return affected > 0 ? "更新成功" : "更新失败";
    }
}
